package org.desktop.web.client.custom.widget;

import java.util.Iterator;

import org.desktop.web.client.custom.widget.grid.user.ModUserAssociationToProjectGrid;
import org.desktop.web.client.user.UserService;
import org.desktop.web.client.user.UserServiceAsync;
import org.desktop.web.shared.domain.entities.Project;
import org.desktop.web.shared.domain.entities.User;

import com.google.gwt.core.shared.GWT;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.button.ButtonBar;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.BorderLayoutContainer;
import com.sencha.gxt.widget.core.client.container.BorderLayoutContainer.BorderLayoutData;
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutPack;
import com.sencha.gxt.widget.core.client.event.SelectEvent;

public class ModUserWindow extends AbstractWindow {

	private ContentPanel panel;
	private Project project;
	private UserServiceAsync userService = GWT.create(UserService.class);
	
	public ModUserWindow(Project p) {
		super();
		this.setWidth(790);
		this.project=p;
		this.setHeight(480);
		this.panel=new ContentPanel();
		this.panel.setHeaderVisible(false);
		BorderLayoutContainer con = new BorderLayoutContainer();

		ContentPanel centerPanel = new ContentPanel();
		
		//pannello centrale
		centerPanel.setHeaderVisible(false);
		final ModUserAssociationToProjectGrid grid = new ModUserAssociationToProjectGrid(this.project);
		centerPanel.add(grid.asWidget());
		BorderLayoutData bld = new BorderLayoutData();
		bld.setSize(1.0);
		con.setCenterWidget(centerPanel,bld);
		
		//pannello sud
		ButtonBar buttonBar = new ButtonBar();
		
		TextButton save = new TextButton("Save");
		//inserisco l'handler
		
		save.addHandler(new com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler() {
			
			@Override
			public void onSelect(SelectEvent event) {
				// TODO chiamata rest per inserire gli utenti selezionati
				grid.getGrid().setLoadMask(true);
				Iterator<User> selected = grid.getSelected().values().iterator();
				Iterator<User> unselected = grid.getUnselected().values().iterator();
				

				while (selected.hasNext()) {
					final User user = (User) selected.next();
//					userService.addUserToProject(user,project,new AsyncCallback<Boolean>() {
//
//						@Override
//						public void onFailure(Throwable caught) {
//							
//							
//						}
//
//						@Override
//						public void onSuccess(Boolean result) {
//							if(!result){
//								Window.alert("Utente "+user.getLogin()+" non �� stato aggiunto perch�� si sono verificati problemi. Contattare l'aministratore");
//								return;
//							}
//							
//							
//						}
//					});
				}
				
			}
		}, SelectEvent.getType());
		buttonBar.add(save);
		addButton(buttonBar);
		
		;
		setButtonAlign(BoxLayoutPack.CENTER);
		
		this.panel.add(con);
		this.add(panel);
		// TODO Auto-generated constructor stub
	}

	@Override
	public String id() {
		// TODO Auto-generated method stub
		return "ADD_REMOVE_USER_PROJECT";
	}




}
